﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using BLL.DataContextClasses;

namespace BLL.BusinessClasses
{
    public class Postgrado
    {
         //Campos
        private int id;
        private string descripcion;
        private AreaAcademica areaAcademica;

        //Propiedades
        public int ID
        {
            get { return this.id; }
        }
        public string Descripcion
        {
            get { return this.descripcion; }
            set { this.descripcion = value; }
        }
        public AreaAcademica AreaAcademica
        {
            get
            {
                if (this.areaAcademica == null) this.areaAcademica = AreaAcademica.GetAreaAcademicaByPostgradoID(this.ID);
                return this.areaAcademica;
        }
            set { this.areaAcademica = value; }
    }

        //Constructores
        public Postgrado(int IDToSearch)
        {
            try
            {
                DataClassesDataContext DB = new DataClassesDataContext();

                DataContextClasses.Postgrado Pos = (from p in DB.Postgrados
                                                  where p.ID == IDToSearch
                                                  select p).First();

                this.id = Pos.ID;
                this.descripcion = Pos.Descripcion;
                this.AreaAcademica = null;
            }
            catch (SqlException SqlEx)
            {
                throw new Exception("No existe tal carrera\n" + SqlEx.Message);
            }
        }

        //Metodos
        public static List<Postgrado> GetPostgradosByEgresadoID(int EgresadoID)
        {
            try
            {
                DataClassesDataContext DB = new DataClassesDataContext();

                var Postgrados = (from p in DB.Egresados_Postgrados
                                  where p.ID_Egresado == EgresadoID
                                  select p);

                List<Postgrado> PostgradosDeEgresado = new List<Postgrado>();

                foreach (var item in Postgrados)
                {
                    Postgrado PostgradoAIntroducir = new Postgrado(item.ID_Postgrado);
                    PostgradosDeEgresado.Add(PostgradoAIntroducir);
                }
                return PostgradosDeEgresado;
            }
            catch (SqlException SqlEx)
            {
                throw new Exception("Hubo un problema con la base de datos\n" + SqlEx.Message);
            }
        }

        public static List<Postgrado> GetPostgradosByVacanteID(int VacanteID)
        {
            try
            {
                DataClassesDataContext DB = new DataClassesDataContext();

                var Postgrados = (from p in DB.Vacantes_Postgrados
                                  where p.ID_Postgrado == VacanteID
                                  select p);

                List<Postgrado> PostgradosDeVacante = new List<Postgrado>();

                foreach (var item in Postgrados)
                {
                    Postgrado PostgradoAIntroducir = new Postgrado(item.ID_Postgrado);
                    PostgradosDeVacante.Add(PostgradoAIntroducir);
                }
                return PostgradosDeVacante;
            }
            catch (SqlException SqlEx)
            {
                throw new Exception("Hubo un problema con la base de datos\n" + SqlEx.Message);
            }
        }
        public static List<Postgrado> GetPostgradosByAreaAcademicaID(int AreaAcademicaID)
        {
            try
            {
                DataClassesDataContext DB = new DataClassesDataContext();

                var IDPostgrados = from p in DB.Postgrados
                                   where p.ID_AreaAcademica == AreaAcademicaID
                                   select p.ID;

                List<Postgrado> PostgradosDeAreaAcademica = new List<Postgrado>();

                foreach (int PostgradoID in IDPostgrados)
                {
                    PostgradosDeAreaAcademica.Add(new Postgrado(PostgradoID));
                }
                return PostgradosDeAreaAcademica;
            }
            catch (SqlException SqlEx)
            {
                throw new Exception("Hubo un problema con la base de datos\n" + SqlEx.Message);
            }
        }
    }
}
